package com.eprize;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class LessThanTwoNeighborsTest {

	private Grid grid;
	private int gridRows = 4;
	private int gridColumns = 4;

	@Before
	public void setup() {
		grid = new Grid(gridRows, gridColumns);
	}

	@Test
	public void live_cell_with_zero_live_neighbors_dies() {
		grid.makeCellAlive(1, 1);

		grid.evolve();

		assertEquals(false, grid.getCell(1, 1));
	}

	@Test
	public void live_cell_with_one_live_neighbor_dies() throws Exception {
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(0, 1);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_two_live_neighbors_in_same_column_lives()
			throws Exception {
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(2, 1);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}

	private void printoutGrid() {
		for (int i = 0; i < gridRows; i++) {
			for (int j = 0; j < gridColumns; j++) {
				System.out.print(grid.getCell(i, j) + " ");
			}
			System.out.println("");			
		}
		System.out.println("------------------------------------------");

	}

	@Test
	public void live_cell_with_two_live_neighbors_in_same_row_lives() {
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(1, 2);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_two_live_neighbors_in_diagonal_left_to_right_lives() {
		grid.makeCellAlive(0, 0);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(2, 2);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_two_live_neighbors_in_diagonal_right_to_left_lives() {
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(2, 0);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_home_cell_should_die_with_zero_neighbors() {
		grid.makeCellAlive(0, 0);
		grid.evolve();
		assertEquals(false, grid.getCell(0, 0));
		printoutGrid();
	}

	@Test
	public void live_grid_max_cell_should_die_with_zero_neighbors() {
		grid.makeCellAlive(gridRows - 1, gridColumns - 1);
		grid.evolve();
		assertEquals(false, grid.getCell(gridRows - 1, gridColumns - 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_eight_neighbors_should_have_count_of_eight() {
		grid.makeCellAlive(0, 0);
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(1, 2);
		grid.makeCellAlive(2, 0);
		grid.makeCellAlive(2, 1);
		grid.makeCellAlive(2, 2);
		grid.makeCellAlive(3, 1);
		grid.makeCellAlive(1, 3);

		assertEquals(8, grid.determineNumberOfLiveNeighbors(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_two_neighbors_should_have_count_of_two() {
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(1, 2);

		assertEquals(2, grid.determineNumberOfLiveNeighbors(1, 1));
		printoutGrid();
	}

	
	@Test
	public void live_cell_with_eight_live_neighbors_dies() {
		grid.makeCellAlive(0, 0);
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.makeCellAlive(1, 2);
		grid.makeCellAlive(2, 0);
		grid.makeCellAlive(2, 1);
		grid.makeCellAlive(2, 2);
		grid.makeCellAlive(3, 1);
		grid.makeCellAlive(1, 3);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_four_neighbors_dies() {
		grid.makeCellAlive(0, 0);
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void live_cell_with_three_neighbors_lives() {
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 0);
		grid.makeCellAlive(1, 1);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void dead_cell_with_three_neighbors_reanimates() {
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.makeCellAlive(1, 0);
		grid.evolve();
		assertEquals(true, grid.getCell(1, 1));
		printoutGrid();
	}
	@Test
	public void dead_cell_with_two_neighbors_stays_dead() {
		grid.makeCellAlive(0, 1);
		grid.makeCellAlive(0, 2);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}

	@Test
	public void dead_cell_with_one_neighbor_stays_dead() {
		grid.makeCellAlive(0, 1);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}
	@Test
	public void dead_cell_with_four_neighbor_stays_dead() {
		grid.makeCellAlive(0, 1);
		grid.evolve();
		assertEquals(false, grid.getCell(1, 1));
		printoutGrid();
	}

	
}
